package com.masterclient.app.modules.income;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselibrary.widget.EnhanceTabLayout;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.coupon.adapter.CouponPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/4 10:43
 * 功能说明：
 */
@Route(path = AroutePath.INCOME_AND_EXPEND_ACTIVITY)
public class IncomeAndExpendActivity extends BaseActivity {

    @BindView(R.id.view_head)
    View viewHead;
    @BindView(R.id.tabLayout)
    EnhanceTabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_total_income)
    TextView tvTotalIncome;
    @BindView(R.id.tv_total_expenses)
    TextView tvTotalExpenses;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_and_expend);
        ButterKnife.bind(this);

        List<Fragment> list = new ArrayList<>();
        WithdrawListFragment fragment1 = WithdrawListFragment.newInstance("0");
        fragment1.setOnRefreshSuccessListener((totalIncome, totalExpenses) -> {
            if (TextUtils.isEmpty(totalIncome) && TextUtils.isEmpty(totalExpenses)){
                viewHead.setVisibility(View.GONE);
            }else {
                viewHead.setVisibility(View.VISIBLE);
                tvTotalIncome.setText(totalIncome);
                tvTotalExpenses.setText(totalExpenses);
            }
        });
        list.add(fragment1);
        WithdrawListFragment fragment2 = WithdrawListFragment.newInstance("1");
        fragment2.setOnRefreshSuccessListener((totalIncome, totalExpenses) -> {
            if (TextUtils.isEmpty(totalIncome) && TextUtils.isEmpty(totalExpenses)){
                viewHead.setVisibility(View.GONE);
            }else {
                viewHead.setVisibility(View.VISIBLE);
                tvTotalIncome.setText(totalIncome);
                tvTotalExpenses.setText(totalExpenses);
            }
        });
        list.add(fragment2);
        WithdrawListFragment fragment3 = WithdrawListFragment.newInstance("2");
        fragment3.setOnRefreshSuccessListener((totalIncome, totalExpenses) -> {
            if (TextUtils.isEmpty(totalIncome) && TextUtils.isEmpty(totalExpenses)){
                viewHead.setVisibility(View.GONE);
            }else {
                viewHead.setVisibility(View.VISIBLE);
                tvTotalIncome.setText(totalIncome);
                tvTotalExpenses.setText(totalExpenses);
            }
        });
        list.add(fragment3);

        String [] sTitle = {"全部","收入","支出"};
        CouponPagerAdapter pagerAdapter = new CouponPagerAdapter(getSupportFragmentManager(), list, sTitle);
        for(int i=0;i<sTitle.length;i++){
            tabLayout.addTab(sTitle[i],i == 0);
        }
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout.getTabLayout()));
        tabLayout.setupWithViewPager(viewPager);
    }

    @OnClick({R.id.rl_back}) void onClick(View view){
        if (view.getId() == R.id.rl_back) {
            finish();
        }
    }
}
